package com.powernode.business.service.impl;

import com.powernode.business.domain.Car;
import com.powernode.business.mapper.CarMapper;
import com.powernode.common.core.domain.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.powernode.business.domain.Rent;
import com.powernode.business.mapper.RentMapper;
import com.powernode.business.service.RentService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class RentServiceImpl implements RentService{

    @Resource
    private RentMapper rentMapper;

    @Autowired
    private CarMapper carMapper;


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return rentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Rent record) {
        return rentMapper.insert(record);
    }

    @Override
    public int insertSelective(Rent record) {
        return rentMapper.insertSelective(record);
    }

    @Override
    public Rent selectByPrimaryKey(Integer id) {
        return rentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Rent record) {
        return rentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Rent record) {
        return rentMapper.updateByPrimaryKey(record);
    }

    @Override
    public int realDoRent(Rent rent) {
        log.info("客户身份证为{}，当前操作员是{}",rent.getCarNumber(),rent.getOpername());
        //先查询车辆表看这个车辆此时是否在出租
        //涉及线程问题，加锁
        synchronized (this){
            Car car = carMapper.selectCarByCarNum(rent.getCarNumber());
            if (ObjectUtils.isEmpty(car) || car.getStatus().equals(1)){
                throw new IllegalArgumentException("该车辆不存在或者已经被出租");
            }
            //修改出租状态
            car.setStatus(1);
            car.setUpdateBy(rent.getOpername());
            car.setUpdateTime(new Date());
            //更新车
            int i = carMapper.updateByPrimaryKeySelective(car);
            if (i > 0){
                //写出租表
                rent.setCreateTime(new Date());
                rent.setCreateBy(rent.getOpername());
                rent.setUpdateBy(rent.getOpername());
                rent.setUpdateTime(new Date());
                rent.setDelFlag("0");
                //设置出租出租状态   已归还1 and 未归还0
                rent.setRentStatus(0);
                //设置门店 根据车辆来限制
                rent.setDeptId(car.getDeptId());
                //插入租车表
                rentMapper.insert(rent);
                return i;
            }
        }
        return 0;
    }


    @Override
    public List<Rent> selectRent(Rent rent, LoginUser loginUser) {
        return rentMapper.selectRent(rent,loginUser.getDeptId());
    }

    /**
     * 删除出租单
     * 先查询所有的出租单
     * 只要未归还则不能删
     * 如果归还了，则可以删除 del_flag = 2
     * @param id
     * @param loginUser
     * @return
     */
    @Override
    public int deleteRent(List<Integer> id, LoginUser loginUser) {
        log.info("删除的出租单id为(),当前用户id为{}",id.toString(),loginUser.getUserId());
        List<Rent> rentList = rentMapper.selectRentById(id);
        if (CollectionUtils.isEmpty(rentList)){
            //如果查询条件出租单未归还则返回影响函数为1则进入到这里返回为0
            return 0;
        }
        long count = rentList.stream().filter(rent -> rent.getRentStatus().equals(0)).count();
        if (count > 0){
            throw new IllegalArgumentException("有出租单状态为未归还，不能删除");
        }
        return rentMapper.delRent(id,loginUser.getUsername());
    }
}
